Imports System.Windows.Forms

Public Class dlgETotalOfDay
    Dim dt As New DataTable
    Dim sql As String
    Dim max As Integer

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        If dlgFindEmployee.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtEmployeeID.Text = dlgFindEmployee.employeeID
            'dlgEmployee.setActionName("Update")
            'dlgEmployee.ShowDialog()
        End If
    End Sub


    Private Sub dlgTotalOfDay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        nupQuantity.Minimum = 0
        max = db.getvalue("select * from Quarters where getdate() between qstartDate and qEndDate ", "Qlimit")
        nupQuantity.Maximum = max
        sql = "select employeeID as 'EmployeeID',TotalOfDay as 'Coupon' from eTotalOfDay"
        bindSQL(sql)
    End Sub
    Private Sub bindSQL(ByVal sql As String)
        Dim bs As New BindingSource
        dt = db.getvalue(sql).Tables(0)
        bs.DataSource = dt
        dgvECoupon.DataSource = bs

        dgvECoupon.Columns(1).ValueType = nupQuantity.GetType
        For i As Integer = 0 To dt.Columns.Count - 1
            dgvECoupon.Columns(i).Name = dgvECoupon.Columns(i).HeaderText
        Next
        For i As Integer = 0 To dt.Rows.Count - 1
            dgvECoupon.Item(0, i).ToolTipText = db.getvalue("select efullname from employee where employeeid = '" & dt.Rows(i).Item("EmployeeID") & "'", "EfullName")
        Next
        'dgvECoupon.Columns("CardNumber").ReadOnly = True
        'dgvECoupon.Columns("Created by").ReadOnly = True
        'dgvECoupon.AllowUserToDeleteRows = False
        dgvECoupon.AllowUserToAddRows = False

    End Sub

    Private Sub dgvECoupon_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvECoupon.CellContentClick

    End Sub

    Private Sub dgvECoupon_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvECoupon.CellEndEdit
        If e.ColumnIndex = 1 Then
            If Not (dgvECoupon.Item(e.ColumnIndex, e.RowIndex).Value >= 0 And dgvECoupon.Item(e.ColumnIndex, e.RowIndex).Value <= max) Then
                MsgBox("Pls set again.")
            End If

        End If
    End Sub



    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        sql = "select employeeID as 'EmployeeID',TotalOfDay as 'Coupon' from eTotalOfDay  where employeeID in (select employeeID from Employee where  EmployeeID like '%" & txtFind.Text.Replace("'", "''") & "%' or eFullname like '%" & txtFind.Text.Replace("'", "''") & "%')"
        bindSQL(Sql)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim sql As String
        If txtEmployeeID.Text.Length > 0 And txtEmployeeID.Text.Length < 10 Then
            Try
                If Len(db.getvalue("select * from employee where employeeID = '" & txtEmployeeID.Text & "'", "employeeID")) = 0 Then
                    MsgBox("EmployeeID is not found.")
                    Exit Sub
                End If
                sql = "insert into eTotalOfDay values('" & txtEmployeeID.Text & "'," & nupQuantity.Value & ")"
                db.update(sql)
                btnSearch_Click(sender, e)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            If dt.Rows.Count > 0 Then
                db.updateTable(dt, sql)
                MsgBox("Saved!!!", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
